import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class TemplateSquare:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/template"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    #页面元素检查
    def TemplateCheckElement(self):
        #检查添加模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button")
        #检查搜索模板输入框
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > input")
        #检查搜索模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > span > button")
        #检查公用库按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-footer > div > div.ant-tabs-nav > div.ant-tabs-nav-wrap > div > div.ant-tabs-tab.ant-tabs-tab-active")
        #检查私有库按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-footer > div > div.ant-tabs-nav > div.ant-tabs-nav-wrap > div > div:nth-child(2)")
        #添加屏幕截图
        chromeDriver.getScreenShot(self.driver)


    def TemplateFuncTest(self):
        #测试添加模板功能
        #点击添加模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()
        #点击保存
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button:nth-child(5)").click()
        #输入模板名称
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("新模板")
        #输入分类
        self.driver.find_element(By.CSS_SELECTOR, "#category").send_keys("新模板")
        #选择添加到共享库
        self.driver.find_element(By.CSS_SELECTOR, "#survey-modal-container > div > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-body > form > div:nth-child(4) > div > div > div > div > label > span.ant-checkbox > input").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()
        #点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR, "#survey-modal-container > div > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.ant-btn-primary").click()
        time.sleep(5)
        #点击关闭按钮
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm.ant-btn-dangerous").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()

        #验证是否添加模板成功
        actual = self.driver.find_element(By.XPATH, "//*[contains(text(), '测试添加模板功能')]").text
        print(f"找到的文本内容: {actual}")
        #断言检查一下是否符合预期
        assert "测试添加模板功能" in actual

        #测试模板搜索功能
        #在输入框输入“新模板”
        self.driver.find_element(By.CSS_SELECTOR, "").send_keys("新模板")
        #点击搜索模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()
        #验证是否检索成功
        #若搜索出显示"暂无数据"则说明搜索功能异常，反之搜索功能成功
        actual = self.driver.find_element(By.XPATH, "").text
        print(f"找到的文本内容: {actual}")
